﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcApplication1.Models;

namespace MvcApplication1.Models
{
    public class nguyenlieuRepository
    {
        public List<nguyenlieu> _nguyenlieu = new List<nguyenlieu>();
        private int _nextId = 1;

        public nguyenlieuRepository()
        {
            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Lẩu thái", don_vi_tinh = "cái", ton_kho = 10, muc_bao_dong = 5 });
            this.Add(new nguyenlieu() {ten_nguyen_lieu = "Mực xào", don_vi_tinh = "dĩa", ton_kho = 10, muc_bao_dong = 5 });
            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Cơm chiên Dương Châu", don_vi_tinh = "dĩa", ton_kho = 10, muc_bao_dong = 5 });
            this.Add(new nguyenlieu() {  ten_nguyen_lieu = "Bia", don_vi_tinh = "chai", ton_kho = 10, muc_bao_dong = 5 });

            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Lẩu thái", don_vi_tinh = "cái", ton_kho = 9, muc_bao_dong = 5 });
            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Mực xào", don_vi_tinh = "dĩa", ton_kho = 9, muc_bao_dong = 5 });
            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Cơm chiên Dương Châu", don_vi_tinh = "dĩa", ton_kho = 9, muc_bao_dong = 5 });
            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Bia", don_vi_tinh = "chai", ton_kho = 9, muc_bao_dong = 5 });

            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Lẩu thái", don_vi_tinh = "cái", ton_kho = 8, muc_bao_dong = 5 });
            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Mực xào", don_vi_tinh = "dĩa", ton_kho = 8, muc_bao_dong = 5 });
            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Cơm chiên Dương Châu", don_vi_tinh = "dĩa", ton_kho = 8, muc_bao_dong = 5 });
            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Bia", don_vi_tinh = "chai", ton_kho = 8, muc_bao_dong = 5 });

            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Lẩu thái", don_vi_tinh = "cái", ton_kho = 7, muc_bao_dong = 5 });
            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Mực xào", don_vi_tinh = "dĩa", ton_kho = 7, muc_bao_dong = 5 });
            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Cơm chiên Dương Châu", don_vi_tinh = "dĩa", ton_kho = 7, muc_bao_dong = 5 });
            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Bia", don_vi_tinh = "chai", ton_kho = 7, muc_bao_dong = 5 });

            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Lẩu thái", don_vi_tinh = "cái", ton_kho = 6, muc_bao_dong = 5 });
            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Mực xào", don_vi_tinh = "dĩa", ton_kho = 6, muc_bao_dong = 5 });
            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Cơm chiên Dương Châu", don_vi_tinh = "dĩa", ton_kho = 6, muc_bao_dong = 5 });
            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Bia", don_vi_tinh = "chai", ton_kho = 6, muc_bao_dong = 5 });

            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Lẩu thái", don_vi_tinh = "cái", ton_kho = 5, muc_bao_dong = 5 });
            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Mực xào", don_vi_tinh = "dĩa", ton_kho = 5, muc_bao_dong = 5 });
            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Cơm chiên Dương Châu", don_vi_tinh = "dĩa", ton_kho = 5, muc_bao_dong = 5 });
            this.Add(new nguyenlieu() { ten_nguyen_lieu = "Bia", don_vi_tinh = "chai", ton_kho = 5, muc_bao_dong = 5 });
        }

        public IQueryable<nguyenlieu> GetAll()
        {
            return _nguyenlieu.AsQueryable();
        }
        public IQueryable<nguyenlieu> Get(int startIndex,int pageSize)
        {
            return _nguyenlieu.AsQueryable().Skip(startIndex).Take(pageSize);
        }
        public nguyenlieu Get(int ma_nguyen_lieu)
        {
            return _nguyenlieu.Find(c => c.ma_nguyen_lieu == ma_nguyen_lieu);
        }
        public nguyenlieu Add(nguyenlieu nl)
        {
            nl.ma_nguyen_lieu = _nextId++;
            _nguyenlieu.Add(nl);
            return nl;
        }

        public void Remove(int ma_nguyen_lieu)
        {
            nguyenlieu nl = _nguyenlieu.Find(c => c.ma_nguyen_lieu == ma_nguyen_lieu);
            _nguyenlieu.Remove(nl);
        }
        public bool Update(nguyenlieu nl)
        {
            int index = _nguyenlieu.FindIndex(c => c.ma_nguyen_lieu == nl.ma_nguyen_lieu);
            if (index == -1)
            {
                return false;
            }
            _nguyenlieu.RemoveAt(index);
            _nguyenlieu.Add(nl);
            return true;
        }
    }
}